package com.sshe.web.action.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * 
 * @author 罗超(455723382@qq.com)<br/>
 *         2013-8-2 下午03:16:42
 */
public class DateJsonValueProcessor implements JsonValueProcessor {

	public static final String Default_DATE_PATTERN = "yyyy-MM-dd hh:mm:ss";
	private DateFormat dateFormat;

	public DateJsonValueProcessor(String pattern) {
		try {
			dateFormat = new SimpleDateFormat(pattern);
		} catch (Exception e) {
			dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);
		}
	}

	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	public Object processObjectValue(String key, Object value,
			JsonConfig jsonConfig) {
		return process(value);
	}

	private Object process(Object value) {
		if (value == null)
			return dateFormat.format(new Date());
		else
			return dateFormat.format((Date) value);
	}

}
